﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WL_Store.BLL;
using WL_Store.Model;

namespace WL_Store.Web.subpage
{
    public partial class Refund : System.Web.UI.Page
    {
        protected RefundBLL bll = new RefundBLL();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {

                BindGoodsType();
                BindRefund();
               
            }
        }

        /// <summary>
        /// 绑定查询所有退款信息数据源
        /// </summary>
        private void BindRefund()
        {
            //获取用户检索的信息
            string comName = txt_comName.Value.Trim();
            int categoryId = Convert.ToInt32(ddl_category.SelectedValue);
            int state = Convert.ToInt32(rbl_state.SelectedValue);
            DateTime? startTradingHour = null;
            if (!string.IsNullOrWhiteSpace(txt_StarttradingHour.Value.Trim()))
            {
                startTradingHour= Convert.ToDateTime(txt_StarttradingHour.Value.Trim());
            }
            DateTime? endTradingHour = null;
            if (!string.IsNullOrWhiteSpace(txt_EndtradingHour.Value.Trim()))
            {
                endTradingHour = Convert.ToDateTime(txt_EndtradingHour.Value.Trim());
            }
            

            RefundSearchModel searchModel = new RefundSearchModel() {
                comName = comName,
                StarttradingHour = startTradingHour,
                EndtradingHour = endTradingHour,
                categoryId =categoryId,
                state=state
            };


            List<RefundModel> list = bll.GetRefunds(searchModel);

            rpt_refund.DataSource = list;
            rpt_refund.DataBind();
        }

        /// <summary>
        /// 绑定商品类别数据源
        /// </summary>
        private void BindGoodsType()
        {
            //读取商品类别信息
            DataTable dt = bll.GetGoodsType();
            //数据源赋值
            ddl_category.DataSource = dt;
            ddl_add_categoryId.DataSource = dt;
            //显示字段
            ddl_category.DataTextField = "category";
            ddl_add_categoryId.DataTextField = "category";
            //值字段
            ddl_category.DataValueField = "categoryId";
            ddl_add_categoryId.DataValueField = "categoryId";
            //绑定
            ddl_category.DataBind();
            ddl_add_categoryId.DataBind();
            //添加全部
            ddl_category.Items.Insert(0, new ListItem("全部", "0"));
        }

        protected void btn_search_Click(object sender, EventArgs e)
        {
            BindRefund();
        }

        protected void lbt_delete_Command(object sender, CommandEventArgs e)
        {
            if (e.CommandName=="del")
            {

                //获取点击的orderId
                string orderId = e.CommandArgument.ToString();
                if (bll.DeleteRefund(orderId))
                {
                    Page.ClientScript.RegisterStartupScript(this.GetType(), "alert", "<script>alert('删除成功')</script>");
                    BindRefund();
                }
                else
                {
                    Page.ClientScript.RegisterStartupScript(this.GetType(), "alert", "<script>alert('删除失败')</script>");
                }
            }
        }

        protected void btn_add_Click(object sender, EventArgs e)
        {
            //获取用户输入的信息
            if (Page.IsValid)
            {
                string orderID = txt_add_orderID.Text.Trim();
                string commodityId = txt_add_commodityId.Text.Trim();
                string comName = txt_add_comName.Text.Trim();
                int categoryId = Convert.ToInt32(ddl_add_categoryId.SelectedValue);
                string transactionAmount = txt_add_transactionAmount.Text.Trim();
                string tradingHour = txt_add_tradingHour.Value.Trim();
                string refundMoney = txt_add_refundMoney.Text.Trim();
                string refundAmount = txt_add_refundAmount.Text.Trim();
                int state = Convert.ToInt32(rbt_add_state.SelectedValue);
                string explain = txt_add_explain.Text.Trim();

                RefundModel model = new RefundModel()
                {
                    orderID = orderID,
                    commodityId = Convert.ToInt32(commodityId),
                    comName = comName,
                    categoryId = categoryId,
                    transactionAmount = Convert.ToDouble(transactionAmount),
                    tradingHour = Convert.ToDateTime(tradingHour),
                    refundMoney = Convert.ToDouble(refundMoney),
                    refundAmount = Convert.ToDouble(refundAmount),
                    state = Convert.ToBoolean(state),
                    explain = explain,
                    isDelete = Convert.ToBoolean(1)
                };

                if (bll.AddRefund(model))
                {
                    Page.ClientScript.RegisterStartupScript(this.GetType(), "alert", "<script>alert('添加成功')</script>");
                    BindRefund();
                }
                else
                {
                    Page.ClientScript.RegisterStartupScript(this.GetType(), "alert", "<script>alert('添加失败')</script>");
                }

            }
        }
    }
}